//
//  ShowTableViewCellCommentView.m
//  Taste
//
//  Created by 徐昊 on 16/9/5.
//  Copyright © 2016年 徐昊. All rights reserved.
//

#import "ShowTableViewCellCommentView.h"
#import "MLLinkLabel.h"
#import "UIView+SDAutoLayout.h"
#import "HXMacro.h"
#import "Comments.h"
#import "ShowRes.h"

@interface ShowTableViewCellCommentView ()<MLLinkLabelDelegate>
@property (nonatomic, strong) NSMutableArray *commentLabelsArray;
@property (nonatomic, strong) NSArray *commentItemsArray;
@end

@implementation ShowTableViewCellCommentView

- (void)didClickLink:(MLLink *)link linkText:(NSString *)linkText linkLabel:(MLLinkLabel *)linkLabel {
    NSLog(@"点你了, 怎么了");
}
//- (instancetype)initWithFrame:(CGRect)frame {
//    self = [super initWithFrame:frame];
//    if (self) {
//        
//    }
//    return self;
//}


- (void)setCommentItemsArray:(NSArray *)commentItemsArray {
    _commentItemsArray = commentItemsArray;
    long originalLabelsCount = self.commentLabelsArray.count;
    long needsToAddCount = commentItemsArray.count > originalLabelsCount ? (commentItemsArray.count - originalLabelsCount) : 0;
    for (int i = 0; i < needsToAddCount; i++) {
        MLLinkLabel *label = [MLLinkLabel new];
        UIColor *highLightColor = TimeLineCellHighlightedColor;
        label.linkTextAttributes = @{NSForegroundColorAttributeName : highLightColor};
        label.font = [UIFont systemFontOfSize:14];
        label.delegate = self;
        [self addSubview:label];
        [self.commentLabelsArray addObject:label];
    }
    if (commentItemsArray.count != 0) {
        
        if ([commentItemsArray[0] isKindOfClass:[ShowRes class]]) {
            for (int i = 0; i < commentItemsArray.count; i++) {
                ShowRes *model = commentItemsArray[i];
                MLLinkLabel *label = _commentLabelsArray[i];
                if (!model.attributedContent) {
                    model.attributedContent = [self generateAttributedStringWithPublishItemModel:model];
                }
                label.attributedText = model.attributedContent;
            }
        } else {
            for (int i = 0; i < commentItemsArray.count; i++) {
                Comments *model = commentItemsArray[i];
                MLLinkLabel *label = _commentLabelsArray[i];
                if (!model.attributedContent) {
                    model.attributedContent = [self generateAttributedStringWithCommentItemModel:model];
                }
                label.attributedText = model.attributedContent;
            }
        }
    }
    
}
- (NSMutableArray *)commentLabelsArray
{
    if (!_commentLabelsArray) {
        _commentLabelsArray = [NSMutableArray new];
    }
    return _commentLabelsArray;
}
- (void)setupWithCommentItemsArray:(NSArray *)commentItemsArray {
    self.commentItemsArray = commentItemsArray;
    if (self.commentLabelsArray.count) {
        [self.commentLabelsArray enumerateObjectsUsingBlock:^(UILabel *label, NSUInteger idx, BOOL *stop) {
            [label sd_clearAutoLayoutSettings];
            // 重用时先隐藏评论label, 然后根据评论个数显示label
            label.hidden = YES;
        }];
    }
    if (!commentItemsArray.count) {
        // 如果没有评论或者点赞，设置commentview的固定宽度为0（设置了fixedWith的控件将不再在自动布局过程中调整宽度）
        self.fixedWidth = @(0);
        // 如果没有评论或者点赞，设置commentview的固定高度为0（设置了fixedHeight的控件将不再在自动布局过程中调整高度）
        self.fixedHeight = @(0);
    } else {
        // 取消固定宽度约束
        self.fixedWidth = nil;
        // 取消固定高度约束
        self.fixedHeight = nil;
    }
    UIView *lastTopView = [[UIView alloc] initWithFrame:CGRectZero];
    for (int i = 0; i < self.commentItemsArray.count; i++) {
        UILabel *label = (UILabel *)self.commentLabelsArray[i];
        label.hidden = NO;
        CGFloat topMargin = (i == 0) ? 10 : 5;
        label.sd_layout
        .leftSpaceToView(self, 8)
        .rightSpaceToView(self, 5)
        .topSpaceToView(lastTopView, topMargin)
        .autoHeightRatio(0);
        
        label.isAttributedContent = YES;
        lastTopView = label;
    }
    [self setupAutoHeightWithBottomView:lastTopView bottomMargin:6];
    
}
- (NSMutableAttributedString *)generateAttributedStringWithPublishItemModel:(ShowRes *)model {
    NSString *text = model.nickname;
    text = [text stringByAppendingString:[NSString stringWithFormat:@"：%@", model.title]];
    NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:text];
    [attString setAttributes:@{NSLinkAttributeName : model.aid} range:[text rangeOfString:model.nickname]];
    return attString;
}
- (NSMutableAttributedString *)generateAttributedStringWithCommentItemModel:(Comments *)model
{
    NSString *text = model.nickname;
    if (model.replayname.length) {
        text = [text stringByAppendingString:[NSString stringWithFormat:@"回复%@", model.replayname]];
    }
    text = [text stringByAppendingString:[NSString stringWithFormat:@"：%@", model.content]];
    NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:text];
    [attString setAttributes:@{NSLinkAttributeName : model.uid} range:[text rangeOfString:model.nickname]];
    if (model.replayname) {
        [attString setAttributes:@{NSLinkAttributeName : model.replayedid} range:[text rangeOfString:model.replayname]];
    }
    return attString;
}
- (void)setFrame:(CGRect)frame {
    [super setFrame:frame];
}

@end
